<template>
  <el-container class="home">
    <el-aside class="aside" :width="!isCollapse ? '200px' : '64px'">
      <el-menu
        :default-active="defaultActive"
        class="el-menu-vertical"
        @open="handleOpen"
        @close="handleClose"
        :collapse="isCollapse"
        router
      >
        <el-submenu :index="'' + item.id" v-for="item in menus" :key="item.id">
          <template slot="title">
            <i class="el-icon-location"></i>
            <span slot="title">{{ item.authName }}</span>
          </template>
          <el-menu-item
            v-for="_item in item.children"
            :key="_item.id"
            :index="'/' + _item.path"
            >{{ _item.authName }}</el-menu-item
          >
        </el-submenu>
      </el-menu>
    </el-aside>
    <el-container>
      <el-header class="header" style="text-align: right; font-size: 12px">
        <i
          class="icon"
          :class="isCollapse ? 'el-icon-s-fold' : 'el-icon-s-unfold'"
          @click="isCollapse = !isCollapse"
        ></i>
        <span>{{ $store.state.username }}</span>
        <el-dropdown>
          <i class="el-icon-setting" style="margin-right: 15px"></i>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="handleSignOut"
              >退出登录</el-dropdown-item
            >
          </el-dropdown-menu>
        </el-dropdown>
        <span>{{ $store.state.usernam }}</span>
      </el-header>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
// import axios from "axios";
import * as api from "@/http/api";
import { mapActions } from "vuex";
export default {
  data() {
    return {
      //是否折叠
      isCollapse: true,
      //权限菜单
      menus: [],
      timer: null,
    };
  },
  computed: {
    defaultActive() {
      if (this.$route.name === "GoodsAdd" || this.$route.name === "GoodsEdit") {
        return "/goods";
      }
      return this.$route.path;
    },
  },
  methods: {
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    },
    ...mapActions(["signOut"]),
    //退出
    handleSignOut() {
      this.$confirm("是否确认退出, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.signOut();
          this.$message({
            type: "success",
            message: "退出成功!",
          });
          this.timer = setTimeout(() => {
            this.$router.replace({
              name: "Login",
            });
          }, 1500);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    },
    //获取左侧权限菜单
    // getMenus() {
    //   axios
    //     .get("http://localhost:8888/api/private/v1/menus", {
    //       headers: {
    //         Authorization:
    //           "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjUwMCwicmlkIjozMCwiaWF0IjoxNjg0ODk3NjkwLCJleHAiOjE2ODQ5ODQwOTB9.Y0PBqrm8iz0ejqQiVnfmadZXCnd1sUzq4_xC4NMePkw",
    //       },
    //     })
    //     .then((res) => {
    //       if ((res.status >= 200 && res.status < 300) || res.status === 304) {
    //         if (res.data.meta.status === 200) {
    //           const {
    //             data: { data },
    //           } = res;
    //           this.menus = data || [];
    //         } else {
    //           this.$message.error(res.data.meta.msg);
    //         }
    //       } else {
    //         this.$message({
    //           message: res.statusText,
    //           type: "error",
    //         });
    //       }
    //     });
    // },
    async getMenus() {
      this.menus = [
        ...((await api.getMenus()) || []),
        {
          authName: "大屏模块",
          id: new Date(),
          path: "screen",
          children: [
            {
              authName: "大屏展示",
              id: new Date(),
              path: "screen",
            },
          ],
        },
      ];
    },
  },
  created() {
    this.getMenus();
  },
};
</script>
<style lang="stylus" scoped>
.home
  height: 100vh
  .aside
    transition: width 0.3s ease-in-out;
  .el-menu-vertical
    height: 100vh
    box-sizing: border-box
  .el-menu-vertical:not(.el-menu--collapse)
    width: 200px
    box-sizing: border-box
  .header
    background-color: #b3c0d1
    color: #333
    line-height: 60px
    display: flex
    align-items: center
    justify-content: flex-end
    .icon
      margin-right: auto
      font-weight: 200
      font-size: 30px
      color: #666
</style>
